package com.yang.qqclient.service;

import com.yang.common.Message;
import com.yang.common.MessageType;
import com.yang.common.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Description: 完成用户的登录和注册
 * @Author: YANG QIANG
 * @Time: 2022-12-19 23:01
 */
public class UserClientService {
    private User user = new User();

    private Socket socket;

    public UserClientService(String userId, String pwd) {
        user.setUserId(userId);
        user.setPwd(pwd);
    }

    public UserClientService() {
    }

    // 验证用户是否登录成功
    public boolean checkUser(){
        boolean b = false;
        try {
            // 连接到服务端
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);

            // 发送user对象给服务器
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);

            // 获取服务器返回的message对象
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message message = (Message) ois.readObject();

            // 判断是否登录成功
            if (MessageType.MESSAGE_LOGIN_SUCCEED.equals(message.getMessageType())){
                // 创建一个和服务器端保持通信的线程 -> 创建一个线程类 ClientConnectServerThread
                ClientConnectServerThread connectServerThread = new ClientConnectServerThread(socket);
                connectServerThread.start();
                // 为了客户端可以有多个用户, 我们把客户端线程放到集合中
                ManageClientConnectServerThread.addClientConnectServerThread(user.getUserId(), connectServerThread);
                b = true;
            }else {
                // 登录失败,关闭socket
                socket.close();
            }

        }catch (Exception e){

        }

        return b;
    }

    // 向服务器获取在线用户列表
    public void onlineFriendList(){
        try {
            // 构建请求获取在线列表的的消息
            Message message = new Message();
            message.setMessageType(MessageType.MESSAGE_GET_ONLINE_FRIEND);
            message.setSender(user.getUserId());
            // 从线程获取用户的通信线程socket
            ClientConnectServerThread connectServerThread = ManageClientConnectServerThread.getClientConnectServerThread(user.getUserId());
            Socket socket = connectServerThread.getSocket();

            // 发送给服务器
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 编写方法,退出客户端,并发送一个退出系统的消息给服务端
    public void logout() {

        Message message = new Message();
        message.setMessageType(MessageType.MESSAGE_CLIENT_EXIT);
        message.setSender(user.getUserId());

        try {
            // 发送给服务端
            ObjectOutputStream oos = new ObjectOutputStream(
                            ManageClientConnectServerThread.getClientConnectServerThread(user.getUserId()).getSocket().getOutputStream());
            oos.writeObject(message);
            System.out.println(user.getUserId() + "退出系统");
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
